var categoryChart = null;
var deviceChart = null;
var provinceChart = null;
var proDefectPartChart;
var proDefectCauseChart;
var secDefectPartChart;
var secDefectCauseChart;
var channelDefPartChart;
var channelDefCauseChart;
var year;

function changeYear() {
	var year = $("#year").val();
	selectYear([{
		name:'year',
		value:year
	}]);
	initDatas();
}

function initDatas() {
	year = $("#year").val();
	getDataByYear(year);
	$('.info-box-content .year').html(year);
	/*$('.info-box-content .defPositionTitle').html(year+'年缺陷部位分布');
	$('.info-box-content .defCauseTitle').html(year+'年缺陷原因分布');*/
}

function getDataByYear(y){
	$.getJSON('/rest/defect/getDefectAnalysisByYear/'+y, function(result) {
		if(!$.isEmptyObject(result)){
			$('.totalNum').html(result.count);
			$('.reality').html(result.reality);
			$('.rate').html(result.rate);
			$('.timelyRate').html(result.timelyRate);
			$('.proNum').html(result.details[0].count);
			$('.proRate').html(result.details[0].rate);
			$('.secNum').html(result.details[1].count);
			$('.secRate').html(result.details[1].rate);
			$('.channelNum').html(result.details[2].count);
			$('.channelRate').html(result.details[2].rate);
			drawChart(result);
		}else{
			$('.totalNum').html(0);
		}
	});
}

function drawChart(data){
	//缺陷类别
	categoryChart = initPieChart('category', data.category, '年缺陷类别分布');
	//缺陷设备
	deviceChart = initPieChart('device', data.device, '年缺陷设备分布');
	//省区
	initProvinceChart(data);
	//保护装置
	proDefectPartChart=initPieChart('protectorDefPosition',data.details[0].values[0],'年缺陷部位分布');
	proDefectCauseChart=initPieChart('protectorDefCause',data.details[0].values[1],'年缺陷原因分布');
	//二次回路
	secDefectPartChart=initPieChart('secondDefPosition',data.details[1].values[0],'年缺陷部位分布');
	secDefectCauseChart=initPieChart('secondDefCause',data.details[1].values[1],'年缺陷原因分布');
	//保护通道
	channelDefPartChart=initPieChart('channelDefPosition',data.details[2].values[0],'年缺陷部位分布');
	channelDefCauseChart=initPieChart('channelDefCause',data.details[2].values[1],'年缺陷原因分布');
}

function initPieChart(chartid, data, title) {
	if (data === undefined || data.length == 0){
		$('#'+chartid).html("该时间暂无数据信息");
	} else {
		var pie = new Pie(chartid);
		pie.setTitle(year+title);
		pie.setPie(data);
		var option = pie.getOption();
		option.title.textStyle.fontSize=16;
		option.title.textStyle.fontWeight='normal';
		option.legend.orient='vertical';
		option.series[0].label.normal.formatter='{d}%';
		pie.setPieOuterRadius('50%');
		pie.setLegendPosition('left','bottom');
		pie.setTitlePosition('center');
		pie.show();
		return pie;
	}
}
function initProvinceChart(data) {
	var p = data.province;
	var lable = p.lables;
	if(lable.length == 0) {
		$("#province").html("该时间暂无数据信息");
	} else {
		var bar = new CategoryBar('province');
		bar.setxAxis(data.province.lables);
		bar.setyAxis(data.province.values);
		bar.setyAxisName("缺陷次数");
		bar.setTitle(year+'年缺陷省区分布');
		bar.setTitlePosition('center');
		var option = bar.getOption();
		option.legend.show = false;
		option.title.textStyle.fontSize=16;
		option.title.textStyle.fontWeight='normal';
		bar.show();
		provinceChart = bar.getChart();
		return provinceChart;
	}
}